#define _CRT_SECURE_NO_WARNINGS 1
#include"string.h"

namespace ST
{
	const size_t string::npos = -1;

	string::string(const char* str)
		:_size(strlen(str))
	{
		_capacity = _size;
		_str = new char[_size + 1];
		memcpy(_str, str, _size + 1);
	}

	string::~string()
	{
		delete[] _str;
		_str = nullptr;
		_size = _capacity = 0;
	}

	//s3(s1)
	/*string::string(const string& s)
	{
		cout << "string::string(const string& s)" << endl;
		_str = new char[s._capacity + 1];
		memcpy(_str, s._str, s._size + 1);
		_size = s._size;
		_capacity = s._capacity;
	}*/

	void string::swap(string& s)
	{
		std::swap(_str, s._str);
		std::swap(_size, s._size);
		std::swap(_capacity, s._capacity);
	}
	//s3(s1)
	string::string(const string& s)
	{
		cout << "string::string(const string& s)" << endl;
		string tmp(s._str);
		swap(tmp);
	}

	//s1=s2;
	/*string& string::operator=(const string& s)
	{
		if (this != &s)
		{
			char* tmp = new char[s._capacity + 1];
			memcpy(tmp, s._str, s._size + 1);
			delete[]_str;
			_str = tmp;
			_size = s._size;
			_capacity = s._capacity;
		}
		return *this;
	}*/
	//s1=s2
	/*string& string::operator+=(const string& s)
	{
		if (this != &s)
		{
			string tmp(s);
			swap(tmp);
		}
		return *this;
	}*/

	string& string::operator=(string tmp)
	{
		cout << "string& string::operator=(string tmp)" << endl;
		swap(tmp);
		return *this;
	}

	string::iterator string::begin()
	{
		return _str;
	}
	string::iterator string::end()
	{
		return _str + _size;
	}

	string::const_iterator string::begin() const
	{
		return _str;
	}

	string::const_iterator string::end()const
	{
		return _str + _size;
	}
}